package com.xpffly.design.iterator.demo.service.impl;

import com.xpffly.design.iterator.demo.service.IProject;
import com.xpffly.design.iterator.demo.service.IProjectIterator;

import java.util.ArrayList;

/**
 * @Author: xpengfei
 * @Date: 2021/4/13 23:23
 * @Description：项目迭代器
 */
public class ProjectIterator implements IProjectIterator {

    /**
     * 将所有的项目信息存放到list中
     */
    private ArrayList<IProject> projectArrayList = new ArrayList<IProject>();
    /**
     * 当前项目索引下标
     */
    private int currentItem = 0;

    /**
     * 构造函数注入projectList
     *
     * @param projectArrayList
     */
    public ProjectIterator(ArrayList<IProject> projectArrayList) {
        this.projectArrayList = projectArrayList;
    }

    /**
     * 判断是否还有元素
     * Returns {@code true} if the iteration has more elements.
     * (In other words, returns {@code true} if {@link #next} would
     * return an element rather than throwing an exception.)
     *
     * @return {@code true} if the iteration has more elements
     */
    public boolean hasNext() {
        return (this.currentItem >= this.projectArrayList.size() ||
                this.projectArrayList.get(this.currentItem) == null)
                ? false : true;
    }

    /**
     * 取下一个值
     * <p>
     * Returns the next element in the iteration.
     *
     * @return the next element in the iteration
     * @throws NoSuchElementException if the iteration has no more elements
     */
    public IProject next() {
        return this.projectArrayList.get(this.currentItem++);
    }

    /**
     * 删除一个对象
     * <p>
     * Removes from the underlying collection the last element returned
     * by this iterator (optional operation).  This method can be called
     * only once per call to {@link #next}.  The behavior of an iterator
     * is unspecified if the underlying collection is modified while the
     * iteration is in progress in any way other than by calling this
     * method.
     *
     * @throws UnsupportedOperationException if the {@code remove}
     *                                       operation is not supported by this iterator
     * @throws IllegalStateException         if the {@code next} method has not
     *                                       yet been called, or the {@code remove} method has already
     *                                       been called after the last call to the {@code next}
     *                                       method
     * @implSpec The default implementation throws an instance of
     * {@link UnsupportedOperationException} and performs no other action.
     */
    public void remove() {

    }
}
